package com.cindata

import grails.transaction.Transactional

@Transactional
class CollateralFlowStageNotificationService
{
    def initFlowStageNotification(Collateral collateral, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.notifications?.each {
                def collateralFlowStageNotification = new CollateralFlowStageNotification()
                collateralFlowStageNotification.role = it?.role
                collateralFlowStageNotification.messageTemplate = it?.messageTemplate
                collateralFlowStageNotification.toManager = it?.toManager
                collateralFlowStageNotification.cellphone = it?.cellphone
                collateralFlowStageNotification.flowStage = CollateralFlowStage.findByCollateralAndStage(collateral, workflowStage?.stage)
                if (collateralFlowStageNotification.validate())
                {
                    collateralFlowStageNotification.save flush: true
                }
                else
                {
                    println collateralFlowStageNotification.errors
                }
            }
        }
    }
}